import { Service, Inject } from 'typedi'
import { Entity } from '@/utils/orm'
import { JsonResult, msgList, res } from '@/utils/jsonResult';
import { Menu } from '@/entity/Menu';
import { addMenuParams } from "@/model/request/Menu";

@Service()
export class MenuDao {

    @Inject()
    orm!: Entity

    @Inject()
    json!: JsonResult<any>;

    async addMenuDao(params: addMenuParams): Promise<res<null>> {
        let res = await this.orm.source()
            .insert()
            .into(Menu)
            .values({
                menu_text: params.text,
                menu_icon: params.icon,
                menu_href: params.path,
                menu_type: params.type,
                menu_father_id: params.father_id,
                menu_status: 0
            })
            .execute();
        return this.json.success(null, msgList.pushMessageSuccess)
    }

    async GetAllMenuDao(): Promise<res<Menu[]>> {
        let res = await this.orm.repository(Menu).getMany();
        let fa: Menu[] = []
        res.forEach(v => {
            if (v.menu_type == 1) {
                fa.push(<Menu>v)
                v.children = []
            }
        })

        res.forEach(j => {
            fa.forEach(v => {
                if (j.menu_father_id == v.menu_id) {
                    v.children.push(<Menu>j)
                }
            })
        })



        return this.json.success(fa);
    }
}